//
//  UIView+Drop.m
//  MGPublic
//
//  Created by lhongl on 2025/11/7.
//

#import "UIView+Drop.h"

@implementation UIView (Drop)

/// 弹出下拉框 【需要知道View的frame】
/// - Parameter point 位置
/// - Parameters dismiss: 点击背景是否消失
- (void)showDropWithPoint:(CGPoint)point dismiss:(BOOL)dismiss{
    UIView * superView = [[[UIApplication sharedApplication] delegate] window];
    self.center = superView.center;
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor clearColor];
    backView.tag = 99999;
    backView.frame = CGRectMake(0, point.y, CGRectGetWidth(superView.bounds), CGRectGetHeight(superView.bounds) - point.y);
    [backView addSubview:self];
    [superView addSubview:backView];
    if (dismiss) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDrop)];
        backView.userInteractionEnabled = YES;
        [backView addGestureRecognizer:tap];
    }
    CGFloat height = CGRectGetHeight(self.frame);
    self.frame = CGRectMake(CGRectGetMinX(self.frame), 0, CGRectGetWidth(self.frame), 0);
    self.alpha = 0.0;
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(CGRectGetMinX(self.frame), 0, CGRectGetWidth(self.frame), height);
        self.alpha = 1.0f;
        backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
}

/// 弹出下拉框
/// - Parameter point 位置
/// - Parameters dismiss: 点击背景是否消失
- (void)showDropNotAnimateWithPoint:(CGPoint)point dismiss:(BOOL)dismiss {
    UIView * superView = [[[UIApplication sharedApplication] delegate] window];
    self.center = superView.center;
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor clearColor];
    backView.tag = 99999;
    backView.frame = CGRectMake(0, point.y, CGRectGetWidth(superView.bounds), CGRectGetHeight(superView.bounds) - point.y);
    [backView addSubview:self];
    [superView addSubview:backView];
    if (dismiss) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDrop)];
        backView.userInteractionEnabled = YES;
        [backView addGestureRecognizer:tap];
    }
    self.frame = CGRectMake(0, 0, CGRectGetWidth(backView.frame), 0);
    self.alpha = 0.0;
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = backView.bounds;
        self.alpha = 1.0f;
        backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
}

/// 消失
- (void)dismissDrop {
    [self dismissDropWithCompletion:nil];
}

- (void)dismissDropWithCompletion:(void(^ __nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:99999];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(CGRectGetMinX(self.frame), 0, CGRectGetWidth(self.frame), 0);
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// 删除
- (void)removeDropView {
    UIView *backView = [self.superview viewWithTag:99999];
    self.alpha = 0.0f;
    backView.alpha = 0.0f;
    [backView removeFromSuperview];
}


@end
